<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="1400" destroy-on-close :close-on-press-escape="mode != 'show' ? false : true" @closed="$emit('closed')" align-center>
        <el-form :model="form" :rules="rules" :disabled="mode=='show'" label-width="130" ref="dialogForm">
            <el-row :gutter="1">
                <el-col :span="6">
                    <el-form-item label="付款单号" prop="pay_code">
                        <el-input v-model="form.pay_code" placeholder="付款单号" clearable></el-input>
                    </el-form-item>
                    </el-col>
                <el-col :span="6">
                    <el-form-item label="供应商" prop="supplier_id">
                        <el-select v-model="form.supplier_id" placeholder="请选择" filterable clearable @change="applyChange">
                        <el-option v-for="item in applyList" :key="item.apply_id" :label="item.apply_name" :value="item.apply_id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="付款日期" prop="business_date">
                        <el-date-picker v-model="form.business_date" type="date" placeholder="请选择付款日期" style="width: 100%;" :value-format="'YYYY-MM-DD'"></el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="应付金额" prop="only_money">
                        <el-input v-model="only_money" placeholder="应付金额" readonly></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="18">
                <el-form-item label="付款" prop="detail">
                    <sc-form-table ref="table" v-model="form.detail" :addTemplate="addTemplate" placeholder="暂无数据" height="250" show-summary @sons="accInput">
                        <el-table-column label="付款人" align="center">
                            <template #default="scope">
                                <el-select v-model="scope.row.settlement_id" placeholder="请选择" filterable>
                                <el-option v-for="item in accountList" :key="item.id" :label="item.account" :value="item.id"/>
                                </el-select>
                            </template>
                        </el-table-column>
                        <el-table-column label="付款金额" prop="money" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.money" placeholder="金额" @input="accInput"></el-input>
                            </template>
                        </el-table-column>
                    </sc-form-table>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="备注" type="textarea"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="附件" prop="enclosure">
                    <sc-upload-file v-model="form.enclosure" :limit="1">
                        <el-button type="primary" icon="el-icon-upload">选择文件</el-button>
                    </sc-upload-file>
            </el-form-item>
            </el-col>
        </el-form>
        <template #footer>
            <el-button @click="visible=false">取 消</el-button>
            <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
        </template>
    </el-dialog>
    <collection v-if="dialog.detail" ref="detailDialog" @success="handleSuccess" @closed="dialog.detail = false" :query="{supplier_id:this.form.supplier_id}"></collection>
</template>

<script>
import Collection from './collection.vue'

export default {
    components: {
        Collection,
    },
    emits: ['success', 'closed'],
    data() {
        return {
            mode: "add",
            titleMap: {
                add: '新增',
                edit: '付款',
                show: '查看'
            },
            visible: false,
            isSaveing: false,
            dialog: {
                detail: false,
            },
            form: {
                business_date: this.moment().format('YYYY-MM-DD'),
                pay_code: "",
                supplier_id: "",
                money: "",
                remark: "",
                enclosure: "",
                status: 1,
                accounts_all: [],
                detail: [],
            },
            addTemplate: {},
            accounts_all: {},
            accountList: [],    //账户
            applyList: [],     //客户
            countorder: "",
            only_money: "",
        }
    },
    mounted() {
        this.getAccount()
        this.getApply()
    },
    methods: {
        //显示
        open(mode = 'add') {
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单提交方法
        submit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                if (this.form.money > this.only_money) {
                    this.$alert("本次付款金额已超出应付金额，请检查", "提示", {type: 'error'})
                } else {
                    this.isSaveing = true;
                    var res = await this.$API.finance.fuorder.add.post(this.form);
                    this.isSaveing = false;
                    if (res.code == 1) {
                    this.$emit('success', this.form, this.mode)
                    this.visible = false;
                    this.$message.success("操作成功")
                    } else {
                    this.$alert(res.message, "提示", {type: 'error'})
                    }
                }
                }
            })
        },
        //表单注入数据
        async setData(data) {
            this.form.id = data.id
            var res = await this.$API.finance.fuorder.detail.get({id: data.id});
            this.form.supplier_id = res.data.supplier_id
            this.form.pay_code = res.data.pay_code
            this.form.business_date = res.data.business_date
            this.form.remark = res.data.remark
            this.form.enclosure = res.data.enclosure
            this.form.status = res.data.status
            this.form.money = res.data.money
            this.form.detail = res.data.detail
            let array = []
            res.data.accounts.map(item => {
                array.push({
                business_date: item.accounts && item.accounts.business_date,
                contract_code: item.accounts && item.accounts.purchase && item.accounts.purchase.code,
                all_money: item.accounts && item.accounts.money,
                only_money: item.accounts && item.accounts.only_money,
                pay_money: item.accounts && item.accounts.pay_money,
                money: item.money,
                type: item.accounts && item.accounts.type,
                })
            })
            this.form.accounts_all = array
            this.applyChange(res.data.supplier_id)
        },
        // 账号
        async getAccount() {
            var res = await this.$API.finance.account.all.get();
            this.accountList = res.data;
        },
        // 客户
        async getApply() {
            var res = await this.$API.apply.apply.all.get();
            this.applyList = res.data;
        },
        checkOrder() {
            if (this.form.supplier_id == '') {
                this.$message.error("请选择供应商")
            } else {
                this.dialog.detail = true
                this.$nextTick(() => {
                this.$refs.detailDialog.open('add')
                })
            }
        },
        handleSuccess(data) {
            let array = []
            data.map(item => {
                array.push({
                accounts_id: item.id,
                business_date: item.business_date,
                contract_code: item.purchase && item.purchase.code,
                all_money: item.money,
                pay_money: item.pay_money,
                only_money: item.only_money,
                remark: item.remark,
                money: item.only_money,
                type: item.type,
                })
            })
            let existingIds = this.form.accounts_all.map(item => item.accounts_id);
            for (let i = array.length - 1; i >= 0; i--) {
                if (existingIds.includes(array[i].accounts_id)) {
                array.splice(i, 1);
                }
            }
            this.form.accounts_all = [...this.form.accounts_all, ...array]
            this.orderInput()
        },
        accInput() {
            let count_price = 0
            this.form.detail.map(item => {
                if (item.money) {
                count_price = Number(count_price) + Number(item.money)
                }
            })
            this.form.money = count_price
        },
        orderInput() {
            let count_price = 0
            this.form.accounts_all.map(item => {
                if (item.money) {
                count_price = Number(count_price) + Number(item.money)
                }
            })
            this.countorder = count_price
        },
        async applyChange(e){
            var res = await this.$API.finance.fuorder.apply.post({supplier_id: e})
            this.only_money = res.data.all_only_money
        },
    }
}
</script>

<style>
</style>
